<template>
  <div class="eulogyManagement wh100">
    <van-nav-bar
      title="悼文管理"
      left-text="返回"
      left-arrow
      :right-text="rightText"
      @click-left="onClickLeft"
      @click-right="navAction"
    />
    <div class="content">
      <div class="tabs">
        <div class="tab" @click="goInfo"><span>简介</span></div>
        <div class="tab" @click="goAlbum"><span>相册</span></div>
        <div class="tab active"><span>悼文</span></div>
        <div class="tab" @click="goMessage"><span>留言</span></div>
      </div>
      <div class="list_content">
        <vantList
          :list="list"
          :loading="loading"
          :finished="finished"
          :refreshing="refreshing"
          @getList="getList"
          @refresh="refresh"
        >
          <van-checkbox-group
            ref="checkboxGroup"
            v-model="selectResult"
            @change="selectChange"
          >
            <div class="list" v-for="(item, index) in list" :key="index">
              <eulogyItem
                :item="item"
                :flag="flag"
                @load="getList"
              ></eulogyItem>
            </div>
          </van-checkbox-group>
        </vantList>
      </div>
    </div>

    <div class="footer">
      <div class="footer_top" v-show="selectResult.length > 0">
        <div class="footer_left" @click="checkAll"><span>全选</span></div>
        <div class="footer_right" @click="deleteEulogy"><span>删除</span></div>
      </div>
      <div class="large_btn" @click="addEulogy"><span>添加悼文</span></div>
    </div>
  </div>
</template>

<script>
import vantList from "@/components/list";
import eulogyItem from "./components/eulogyItem";
import { getList, remove } from "@/api/my/eulogyManagement";
import { fetchMediaAuth } from "@/api/memorialHall";
export default {
  data() {
    return {
      flag: false,
      loading: false,
      finished: false,
      refreshing: false,
      actionAuth: false, //操作权限
      pageSize: 10,
      currentPage: 1,
      memoriaId: "",
      selectResult: [],
      list: [],
    };
  },
  components: {
    vantList,
    eulogyItem,
  },
  created() {
    console.log(this.$route.params.id, "悼文编辑");
    this.memoriaId = this.$route.params.id;
    this.getList();
    //获取相册操作权限
    this.getMediaAuth(this.$route.params.id);
  },
  computed: {
    rightText() {
      if (this.flag && this.actionAuth) {
        return "完成";
      } else if (this.actionAuth) {
        return "编辑";
      } else {
        return "";
      }
    },
  },
  methods: {
    //重新加载
    reload(){
      location.reload()
    },
    //获取相册操作权限
    async getMediaAuth(id) {
      let params = {
        id,
      };
      let res = await fetchMediaAuth(params);
      console.log(res, "操作全先");
      if (res.code != "2000") {
        this.$toast.fail(res.msg || "请求数据失败");
        return;
      } else {
        this.actionAuth = res.data;
      }
    },
    //返回纪念馆
    onClickLeft() {
      this.$router.push(`/memorialHall/${this.$route.params.id}`);
    },
    //
    goInfo() {
      this.$router.push(`/memorialHall/albumLife/${this.$route.params.id}`);
    },
    //去相册
    goAlbum() {
      this.$router.push(`/memorialHall/album/${this.$route.params.id}`);
    },
    //去留言
    goMessage() {
      this.$router.push(
        `/memorialHall/messageManagement/${this.$route.params.id}`
      );
    },
    //点击编辑或完成
    navAction() {
      this.flag = !this.flag;
      this.selectResult = [];
      if (this.flag) {
        //完成
      } else {
        //编辑
      }
    },
    //选中改变
    selectChange(e) {
      console.log(e, "选中");
    },
    //全选
    checkAll() {
      //console.log(this.$refs.checkboxGroup)
      this.$refs.checkboxGroup.toggleAll(true);
    },
    //删除
    deleteEulogy() {
      this.$dialog
        .confirm({
          title: "删除操作",
          message: "您确定删除吗？",
        })
        .then(() => {
          // on confirm
          // on confirm
          let ids = this.selectResult.map((el) => el);
          remove({ ids }).then(() => {
            this.$toast.success("删除成功!");
            this.getList();
          });
        })
        .catch(() => {
          // on cancel
          this.$toast("取消删除");
        });
    },

    //请求数据
    async getList() {
      
       this.loading=true
      const { currentPage, pageSize } = this;
      let params = {
        currentPage,
        pageSize,
        contentType: "dw",
        memoriaId: this.memoriaId,
      };
      let res = await getList(params);
     
      console.log(res, "00000");
      if (res.code === "2000") {
        let list = res.data || [];
        //根据总条数处理
        list = list.map((el) => {
          let {
            id,
            content,
            gmtCreate,
            operatorNick,
            operatorVia,
            title,
            state,
          } = el;
          return {
            id,
            name: operatorNick,
            date: gmtCreate,
            avatar: operatorVia,
            status: state,
            title,
            des: content,
          };
        });
        this.list = [...this.list, ...list];
        this.currentPage++;
        console.log(this.list.length, res.total, this.list.length < res.total);
        if (this.list.length >= res.total) {
          console.log("测试");
          this.finished = true;
        }
      }
       this.loading = false;
      this.refreshing = false;
    },
    //下拉刷新
    refresh() {
      this.refreshing = true;
      // 清空列表数据
      this.currentPage = 1;
      this.list = [];
      this.finished = false;
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.getList();
    },
    addEulogy() {
      this.$router.push(`/memorialHall/addEulogy/${this.$route.params.id}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.eulogyManagement {
  display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex; 
display: flex;
  -moz-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
  /deep/ .van-nav-bar {
    background-color: $defaultColor;
    .van-icon,
    .van-nav-bar__text {
      color: #fff;
    }
  }
  .content {
    flex: 1;
    display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex; 
display: flex;
    -moz-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
    //   padding:0.12rem;
    overflow: hidden;
    background: #f9f9f9;
    .tabs {
      display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex; 
display: flex;
      background: #fff;
      -moz-align-items: center;
-webkit-align-items: center;
align-items: center;
      justify-content: space-around;
      margin-bottom: 0.1rem;
      padding: 0.1rem 0;
      .tab {
        width: 0.8rem;
        height: 0.3rem;
        border-radius: 0.05rem;
        background: #f5f5f5;
        color: #b7b7b7;
        display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex; 
display: flex;
        -moz-justify-content: center;
-webkit-justify-content: center;
justify-content: center;
        -moz-align-items: center;
-webkit-align-items: center;
align-items: center;
        span {
          font-size: 15px;
        }
      }
      .tab.active {
        background: $defaultColor;
        span {
          color: #fff;
        }
      }
    }
    .list_content {
      flex: 1;
      overflow-y: scroll;
      padding: 0 0.125rem;
      -webkit-overflow-scrolling: touch;
      // padding:0.125rem 0;
      /deep/ .van-list {
        width: 100%;
      }
    }
  }

  .footer {
    padding: 0 0.12rem;
    .footer_top {
      width: 100%;
      height: 0.5rem;
      display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex; 
display: flex;
      -moz-align-items: center;
-webkit-align-items: center;
align-items: center;
      -moz-justify-content: space-between;
-webkit-justify-content: space-between;
justify-content: space-between;
    }
    .large_btn {
      margin-bottom: 0.1rem;
    }
  }
}
</style>
